# encoding:utf-8
'''
Created on 2022-04-06

@author: adog
'''
import util.dbcom as dbcom
import pandas as pd
import json
import util.xtool  as xtool
import time

views_fields = [
                'ownerId',
                'chartName',
                'ownerSource',
                'symbol',
                'resolution',
                'lastModified',
                'note',
                'lastUpdateTime',
                'cloneOther'
                ]


class ViewPoint():
    
    def __init__(self):
        self.notes = pd.DataFrame([])
    
    def loadTVChart(self):
        try:
            sql = 'select * from model_chart where name like "#%" and isnull(cloneOther);'
            data = pd.DataFrame(dbcom.select_tuple_dict(sql))
            
            for i in data.index:
                ownerId = data.loc[i, 'ownerId']
                chartName = data.loc[i, 'name']
                resolution = data.loc[i, 'resolution']
                lastModified = str(data.loc[i, 'lastModified']).split('.')[0]
                ownerSource = data.loc[i, 'ownerSource']
                cloneOther = data.loc[i, 'cloneOther']
                content = json.loads(json.loads(json.loads(data.loc[i, 'content']))['content'])
                
                for j in range(len(content['charts'])):
                    chart = content['charts'][j]
                    panes = chart['panes']
                    for  k in range(len(panes)):
                        sources = panes[k]['sources']
                        
                        for z in range(len(sources)):
                            if (type(sources[z]) == type({})) and  \
                                ((sources[z]['type'] == 'LineToolNote') or 
                                (sources[z]['type'] == 'LineToolCallout')):
                                # ((sources[z]['type'] == 'LineToolNoteAbsolute') or \
                                # (sources[z]['type'] == 'LineToolTextAbsolute') or \
                                # (sources[z]['type'] == 'LineToolText') or    \
                                
                                note = sources[z]['state']['text']
                                symbol = sources[z]['state']['symbol']
                                lastUpdateTime = lastModified if sources[z]['state']['lastUpdateTime'] == 0 else xtool.timeLong2Str(int(sources[z]['state']['lastUpdateTime'] / 1000), format='sec')
                                # location in chart
                                # points=sources[z]['points']
                                
                                record = pd.DataFrame([[
                                            ownerId,
                                            chartName,
                                            ownerSource,
                                            symbol,
                                            resolution,
                                            lastModified,
                                            note,
                                            lastUpdateTime,
                                            cloneOther
                                            ]], columns=views_fields
                                            )
                                
                                if self.notes is None: 
                                    self.notes = record
                                else:
                                    self.notes = self.notes.append(record)
            if len(self.notes) > 0: 
                self.notes.sort_values(by=['lastUpdateTime'], ascending=[False], inplace=True)
                self.notes.reset_index(drop=True, inplace=True)
            print(self.notes.shape)
            
        except Exception as e:
            print(e)

    def synZhihu2TV(self): 
        sql = 'select * from zhihu_notes'
        data = pd.DataFrame(dbcom.select_tuple_dict(sql)) 
        
        
if __name__ == '__main__':
    mv = ViewPoint()
    mv.loadTVChart()
    
    print(1)
